package com.csweimei.rentmanagementtreasure;

import static com.csweimei.rentmanagementtreasure.weight.UserAgreementDialog.IS_CONFIRM_USER_AGREEMENT;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NavUtils;
import androidx.lifecycle.ViewModelProviders;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.csweimei.rentmanagementtreasure.base.AppViewModelFactory;
import com.csweimei.rentmanagementtreasure.databinding.ActivitySplashBinding;
import com.csweimei.rentmanagementtreasure.viewModel.SplashVM;
import com.csweimei.rentmanagementtreasure.weight.UserAgreementDialog;
import com.gtdev5.geetolsdk.mylibrary.util.SpUtils;
import com.yuanli.ad.holder.TTAdManagerHolder;
import com.yuanli.ad.utils.NewInsertAdUtils;
import com.yuanli.ad.utils.SplashAdUtils;

import me.goldze.mvvmhabit.base.BaseActivity;

public class SplashActivity extends BaseActivity<ActivitySplashBinding, SplashVM> {

    UserAgreementDialog dialog,dialog2;


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_splash;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public SplashVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this,factory).get(SplashVM.class);
    }

    public void restartApp() {
        Intent intent = new Intent(this, SplashActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.setContext(this);
        boolean isAgree = SpUtils.getInstance().getBoolean(IS_CONFIRM_USER_AGREEMENT,false);
        if (!isAgree) {
            // 还未同意过协议
            showUserDialog();
        } else {
            // 同意协议
            toMain();
        }
    }

    /**
     * 用户协议和隐私政策s
     */
    private void showUserDialog() {
        if (dialog == null) {
            dialog = new UserAgreementDialog(this, new UserAgreementDialog.onDialogClickListener() {
                @Override
                public void onConfirm() {
//                    UserUtils.setFreeNum(3);
                    // 进同意协议获取权限的逻辑
                    toMain();
                }

                @Override
                public void onCancel() {
                    // 进第二次弹窗
                    showUserDialog2();
                }
            });
        }
        if (!dialog.isShowing() && !isFinishing()) {
            dialog.show();
        }

    }

    /**
     * 用户协议和隐私政策s
     */
    private void showUserDialog2() {
        if (dialog2 == null) {
            dialog2 = new UserAgreementDialog(this, new UserAgreementDialog.onDialogClickListener() {
                @Override
                public void onConfirm() {
                    showUserDialog();
                }

                @Override
                public void onCancel() {
                    // 关闭应用
                    finish();
                }
            });
        }
        if (!dialog2.isShowing() && !isFinishing()) {
            dialog2.show();
            String hint = "    您的信任对我们非常重要，我们将竭力为您的信息提供保护，请您阅读并同意《用户协议》和《隐私政策》后再开始使用我们的服务。";
            dialog2.setButtonText("退出应用", "查看协议", false, hint);
        }

    }

    private void toMain() {
        MyApplication.getInstance().initUmeng(SplashActivity.this);
        viewModel.bindDevice();
        TTAdManagerHolder.init(this);
        new NewInsertAdUtils().loadInsertAd(this, new NewInsertAdUtils.AdStateListener() {
            @Override
            public void onSuccses() {

            }

            @Override
            public void onError(String msg) {
                viewModel.bindDevice();
            }

            @Override
            public void onClose() {
                viewModel.bindDevice();
            }

            @Override
            public void onRenderFail(String msg) {
                viewModel.bindDevice();
            }
        });

//        new SplashAdUtils().loadSplashAd(this, new SplashAdUtils.AdStateListener() {
//            @Override
//            public void onAdLoadSuccess(View view) {
//                binding.adView.removeAllViews();
//                binding.adView.addView(view);
//            }
//
//            @Override
//            public void onClose() {
//                viewModel.bindDevice();
//            }
//        });
    }
}